//
//  TempateCareOne.m
//  LeaveLight
//
//  Created by pro1 on 2019/3/11.
//  Copyright © 2019 xsl. All rights reserved.
//

#import "TempateCareOne.h"
#import "UITextField+ValueWidth.h"

#define TempateAutherWidth (self.frame.size.width - 16)

@interface TempateCareOne ()<QMUITextViewDelegate,UITextFieldDelegate>

@property (strong,nonatomic) UIView *textFieldLeftView;
@property (strong,nonatomic) UIView *lineLeft;
@property (strong,nonatomic) UIView *textFieldRightView;
@property (strong,nonatomic) UIView *lineRight;

@end

@implementation TempateCareOne

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setShadow:CGSizeMake(0,2) corner:10 opacity:1 color:[UIColor whiteColor] shadowColor:RGB(153, 153, 153) shadowRadius:6];
        
        [self addSubview:self.contentView];
        [self addSubview:self.collection];
        [self addSubview:self.share];
        [self addSubview:self.borderView];
        [self addSubview:self.bottomView];
        [self addSubview:self.timeLabel];
        
        
        [self addSubview:self.logoTip];
        
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self).offset(0);
        }];
        
        [_share mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
            make.width.height.offset(40);
            make.right.equalTo(self).offset(0);
        }];
        
        [_collection mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
            make.width.height.equalTo(self.share);
            make.right.equalTo(self.share.mas_left).offset(0);
        }];
        
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView.mas_bottom).offset(0);
            make.left.right.bottom.equalTo(self).offset(0);
        }];
        
        [_logoTip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-10);
            make.left.right.equalTo(self).offset(0);
            make.height.offset(30);
        }];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
            make.left.equalTo(self).offset((self.frame.size.width - 150)/2);
            make.right.equalTo(self).offset(-(self.frame.size.width - 150)/2);
            make.height.offset(75);
        }];
        
        [_borderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.timeLabel.mas_top).offset(0);
            make.left.equalTo(self).offset((self.frame.size.width - 150)/2);
            make.right.equalTo(self).offset(-(self.frame.size.width - 150)/2);
            make.height.offset(150);
        }];
        
    }
    return self;
}

- (QMUIZoomImageView *)contentView{
    if (!_contentView) {
        _contentView = [[QMUIZoomImageView alloc] init];
        _contentView.viewportRect = CGRectZero;
        _contentView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _contentView;
}

- (UIButton *)collection{
    if (!_collection) {
        _collection = [[UIButton alloc] init];
        [_collection setImage:[UIImage imageNamed:@"btn_home_collect"] forState:UIControlStateNormal];
        [_collection setImage:[UIImage imageNamed:@"btn_home_collect_sel"] forState:UIControlStateSelected];
        _collection.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_collection addTarget:self action:@selector(collectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collection;
}

- (UIButton *)share{
    if (!_share) {
        _share = [[UIButton alloc] init];
        [_share setImage:[UIImage imageNamed:@"btn_home_share"] forState:UIControlStateNormal];
        _share.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_share addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _share;
}

- (UIView *)borderView{
    if (!_borderView) {
        _borderView = [[UIView alloc] init];
        _borderView.backgroundColor = [UIColor clearColor];
        _borderView.layer.borderWidth = 3.0;
        _borderView.layer.borderColor = [UIColor whiteColor].CGColor;
        _borderView.userInteractionEnabled = NO;
    }
    return _borderView;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont boldSystemFontOfSize:30];
        _timeLabel.numberOfLines = 2;
        _timeLabel.backgroundColor = [UIColor whiteColor];
        _timeLabel.text = @"March 20\n2019";
        _timeLabel.userInteractionEnabled = NO;
    }
    return _timeLabel;
}

- (QMUITextView *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[QMUITextView alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = [UIColor blackColor];
        _contentLabel.text = @"极具人性化，其追求的是与数码绘图板极好的相互兼容性、绘图的美感、简便的操作以及为用户提供一个轻松绘图的平台。极具人性化，其追求的是与数码绘图板极好的相互兼容性";
        _contentLabel.delegate = self;
        _contentLabel.maximumHeight = 170;
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.maximumTextLength = 50;
//        _contentLabel.shouldCountingNonASCIICharacterAsTwo = YES;
        NSMutableParagraphStyle * style = [NSMutableParagraphStyle new];
        style.lineSpacing = 10;
        _contentLabel.typingAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 10],NSKernAttributeName: @(-1),NSParagraphStyleAttributeName: style};
    }
    return _contentLabel;
}

- (CHYTextField *)authorTextField{
    if (!_authorTextField) {
        _authorTextField = [[CHYTextField alloc] init];
        _authorTextField.font = [UIFont systemFontOfSize:12];
        _authorTextField.textColor = [UIColor blackColor];
        _authorTextField.text = @"xiaoxiaoSu";
        _authorTextField.leftView = self.textFieldLeftView;
        _authorTextField.rightView = self.textFieldRightView;
        _authorTextField.maxLength = 20;
        weakify(self);
        _authorTextField.inputTextBlock = ^(NSString * _Nonnull inputText) {
            strongify(self);
            if (self.baseModel.template_type == 1) {
                self.textFieldLeftView.frame = CGRectMake(0, 0,TempateAutherWidth - [self.authorTextField getContentStrWidth] + 15, self.authorTextField.font.lineHeight);
            }
            else if (self.baseModel.template_type == 2){
                self.textFieldLeftView.frame = CGRectMake(0, 0, (TempateAutherWidth - [self.authorTextField getContentStrWidth])/2, self.authorTextField.font.lineHeight);
                self.textFieldRightView.frame = CGRectMake(0, 0, (TempateAutherWidth - [self.authorTextField getContentStrWidth])/2, self.authorTextField.font.lineHeight);
            }
            else if (self.baseModel.template_type == 3){
                self.textFieldLeftView.frame = CGRectMake(0, 0,TempateAutherWidth - [self.authorTextField getContentStrWidth] + 15, self.authorTextField.font.lineHeight);
            }
            self.baseModel.provenance = inputText;
        };
    }
    return _authorTextField;
}

- (LogoTipView *)logoTip{
    if (!_logoTip) {
        _logoTip = [[LogoTipView alloc] init];
        _logoTip.style = LogoTipViewStyleQrCode;
    }
    return _logoTip;
}

- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor clearColor];
        _bottomView.clipsToBounds = YES;
        
        [_bottomView addSubview:self.contentLabel];
        [_bottomView addSubview:self.authorTextField];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView).offset(10);
            make.left.equalTo(self.bottomView).offset(23);
            make.right.equalTo(self.bottomView).offset(-8);
            make.height.offset(85);
        }];
        
        [_authorTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(5);
            make.right.equalTo(self.bottomView).offset(-8);
            make.left.equalTo(self.bottomView).offset(8);
            make.bottom.equalTo(self.bottomView).offset(-50);
        }];
    }
    return _bottomView;
}

- (UIView *)textFieldLeftView{
    if (!_textFieldLeftView) {
        _textFieldLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, (TempateAutherWidth - [self.authorTextField getContentStrWidth])/2, self.authorTextField.font.lineHeight)];
        self.lineLeft = [[UIView alloc] init];
        _lineLeft.backgroundColor = [UIColor blackColor];
        [_textFieldLeftView addSubview:_lineLeft];
        [_lineLeft mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textFieldLeftView).offset((self.authorTextField.font.lineHeight - 1)/2);
            make.right.equalTo(self.textFieldLeftView).offset(0);
            make.height.offset(1.0);
            make.width.offset(10);
        }];
    }
    return _textFieldLeftView;
}

- (UIView *)textFieldRightView{
    if (!_textFieldRightView) {
        _textFieldRightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, (TempateAutherWidth - [self.authorTextField getContentStrWidth])/2, self.authorTextField.font.lineHeight)];
        self.lineRight = [[UIView alloc] init];
        _lineRight.backgroundColor = [UIColor blackColor];
        [_textFieldRightView addSubview:_lineRight];
        [_lineRight mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textFieldRightView).offset((self.authorTextField.font.lineHeight - 1)/2);
            make.left.equalTo(self.textFieldRightView).offset(0);
            make.height.offset(1.0);
            make.width.offset(10);
        }];
    }
    return _textFieldRightView;
}

- (void)setCardModel:(TemplateModel *)baseModel{
    self.baseModel = baseModel.chicken_model;
    self.contentLabel.userInteractionEnabled = baseModel.chicken_model.is_edit;
    self.contentView.userInteractionEnabled = baseModel.chicken_model.is_edit;
    self.authorTextField.userInteractionEnabled = baseModel.chicken_model.is_edit;
    
    self.contentLabel.text = baseModel.chicken_model.content;
    self.authorTextField.text = StringIsEmpty(baseModel.chicken_model.provenance) ? @"留光APP" : baseModel.chicken_model.provenance;
    
    self.contentLabel.scrollsToTop = YES;
    [self.contentLabel setContentOffset:CGPointMake(0, 0)];
    
    self.collection.selected = self.baseModel.is_collect == 1 ? NO : YES;
    
    if ([[self getViewcontrollerView:self] isKindOfClass:[EditController class]]) {
        self.share.hidden = YES;
        self.collection.hidden = YES;
    }else{
        self.share.hidden = NO;
        self.collection.hidden = NO;
    }
    
    [self.contentView.imageView sd_setImageWithURL:[NSURL URLWithString:baseModel.chicken_model.images] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        self.contentView.image = image;
    }];
    
    if (baseModel.choiseImage) {
        self.contentView.image = baseModel.choiseImage;
    }
    self.backgroundColor = baseModel.bgColor ? baseModel.bgColor : [UIColor whiteColor];
    self.contentLabel.font = baseModel.font ? [baseModel.font fontWithSize:14] : [UIFont systemFontOfSize:14];
    self.authorTextField.font = baseModel.font ? [baseModel.font fontWithSize:12] : [UIFont systemFontOfSize:12];
    self.logoTip.font = baseModel.font ? [baseModel.font fontWithSize:12] : [UIFont systemFontOfSize:12];
    self.logoTip.style = baseModel.thescaleStyle ? baseModel.thescaleStyle : LogoTipViewStyleText;
    
    [self uploadUIWIth:baseModel.chicken_model];
}

/**
 更新UI

 @param baseModel ZDBaseModel
 */
- (void)uploadUIWIth:(ZDBaseModel *)baseModel{
    if (baseModel.template_type == 1) {
        self.borderView.hidden = NO;
        self.timeLabel.hidden = NO;
        self.timeLabel.backgroundColor = [UIColor whiteColor];
        self.timeLabel.textColor = [UIColor blackColor];
        self.timeLabel.textAlignment = NSTextAlignmentLeft;
        self.authorTextField.textAlignment = NSTextAlignmentRight;
        self.textFieldLeftView.frame = CGRectMake(0, 0,TempateAutherWidth - [self.authorTextField getContentStrWidth] + 15, self.authorTextField.font.lineHeight);
        self.authorTextField.leftViewMode = UITextFieldViewModeAlways;
        self.authorTextField.rightViewMode = UITextFieldViewModeNever;
        self.timeLabel.text = [SystemManager getDateWith:@"\n"];
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(0);
        }];
        
        [_logoTip mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-10);
            make.left.right.equalTo(self).offset(0);
            make.height.offset(30);
        }];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView).offset(10);
        }];
        
        [_authorTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(5);
            make.bottom.equalTo(self.bottomView).offset(-50);
        }];
        
        [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
            make.left.equalTo(self).offset((self.frame.size.width - 150)/2);
            make.right.equalTo(self).offset(-(self.frame.size.width - 150)/2);
            make.height.offset(75);
        }];
        
        [_lineLeft mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(32);
        }];
        
        [self layoutIfNeeded];
        [self.contentView setTopCorner:10];
    }
    else if (baseModel.template_type == 2){
        self.logoTip.style = LogoTipViewStyleText;
        self.borderView.hidden = YES;
        self.timeLabel.hidden = NO;
        self.timeLabel.backgroundColor = UIColorMakeWithHex(@"#FFAF04");
        self.timeLabel.textColor = [UIColor whiteColor];
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.authorTextField.textAlignment = NSTextAlignmentCenter;
        self.authorTextField.leftViewMode = UITextFieldViewModeAlways;
        self.authorTextField.rightViewMode = UITextFieldViewModeAlways;
        self.textFieldLeftView.frame = CGRectMake(0, 0, (TempateAutherWidth - [self.authorTextField getContentStrWidth])/2, self.authorTextField.font.lineHeight);
        self.textFieldRightView.frame = CGRectMake(0, 0, (TempateAutherWidth - [self.authorTextField getContentStrWidth])/2, self.authorTextField.font.lineHeight);
        self.timeLabel.text = [SystemManager getDateWith:@","];
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-70);
        }];
        
        [_logoTip mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-50);
            make.right.equalTo(self).offset(-8);
            make.width.offset(11);
        }];
        
        [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView).offset(45);
        }];
        
        [_authorTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(20);
            make.bottom.equalTo(self.bottomView).offset(-20);
        }];
        
        [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView.mas_bottom).offset(-15);
            make.right.equalTo(self).offset(-8);
            make.width.offset([self retrunTimeStrWidthWith:self.timeLabel.text] + 20);
            make.height.offset(40);
        }];
        [_lineLeft mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(10);
        }];
        [self layoutIfNeeded];
        [self.contentView setTopLeftCorner:10];
    }
    else if (baseModel.template_type == 3){
        self.borderView.hidden = YES;
        self.timeLabel.hidden = YES;
        self.authorTextField.textAlignment = NSTextAlignmentRight;
        self.authorTextField.leftViewMode = UITextFieldViewModeAlways;
        self.authorTextField.rightViewMode = UITextFieldViewModeNever;
        self.textFieldLeftView.frame = CGRectMake(0, 0,TempateAutherWidth - [self.authorTextField getContentStrWidth] + 15, self.authorTextField.font.lineHeight);
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(0);
        }];
        
        [_logoTip mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-10);
            make.right.left.equalTo(self).offset(0);
            make.height.offset(30);
        }];
        
        [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView).offset(15);
        }];
        
        [_authorTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(5);
            make.bottom.equalTo(self.bottomView).offset(-70);
        }];
        [_lineLeft mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(32);
        }];
        [self layoutIfNeeded];
        [self.contentView setTopCorner:10];
    }
    else if (baseModel.template_type == 6){
        self.borderView.hidden = YES;
        self.timeLabel.hidden = YES;
        self.authorTextField.textAlignment = NSTextAlignmentCenter;
        self.authorTextField.leftViewMode = UITextFieldViewModeNever;
        self.authorTextField.rightViewMode = UITextFieldViewModeNever;
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(0);
        }];
        
        [_logoTip mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-10);
            make.right.left.equalTo(self).offset(0);
            make.height.offset(30);
        }];
        
        [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView).offset(0);
        }];
        
        [_authorTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(25);
            make.bottom.equalTo(self.bottomView).offset(-115);
        }];
        [self layoutIfNeeded];
        CAShapeLayer *circleLayer = [CAShapeLayer layer];
        CGPoint fCirclrCenter = self.contentView.center;
        circleLayer.path = ([UIBezierPath bezierPathWithArcCenter:fCirclrCenter radius:110 startAngle:0 endAngle:2*M_PI clockwise:YES]).CGPath;
        self.contentView.layer.mask = circleLayer;
    }
    [self.bottomView setBottomCorner:10.0];
}

#pragma mark - <QMUITextViewDelegate>

- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height{
    if (height > 85) {
//        [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.offset(height);
//        }];
    }
}

- (CGFloat)retrunTimeStrWidthWith:(NSString *)text{
    return [text sizeWithAttributes:@{NSFontAttributeName:self.timeLabel.font}].width;
}

#pragma mark - action
- (void)shareBtnClick:(UIButton *)btn
{
    weakify(self);
    self.share.hidden = YES;
    self.collection.hidden = YES;
    UIImage * shareimg = [self qmui_snapshotImageAfterScreenUpdates:YES];
    self.share.hidden = NO;
    self.collection.hidden = NO;
    
    [ShareView initWithShareChooseViewChooseBlock:^(NSInteger selIndex) {
        if (selIndex < 999 && [[UMSocialManager defaultManager] isInstall:selIndex]) {
            UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
            UMShareImageObject * shareObject = [UMShareImageObject shareObjectWithTitle:@"留光" descr:@"一抹时光 一抹情" thumImage:shareimg];
            shareObject.shareImage = shareimg;
            //分享消息对象设置分享内容对象
            messageObject.shareObject = shareObject;
            [[UMSocialManager defaultManager] shareToPlatform:selIndex messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
                if (error) {
                    [QMUITips showError:@"分享失败"];
                }else{
                    [QMUITips showSucceed:@"分享成功"];
                }
            }];
        }else if(selIndex == 999){
            //保存本地
            strongify(self);
            UIImageWriteToSavedPhotosAlbum(shareimg, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
        }else{
            [QMUITips showWithText:@"分享失败"];
        }
    }];
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error == nil) {
        [QMUITips showSucceed:@"保存成功"];
    }else{
        [QMUITips showError:@"保存失败"];
    }
}


- (void)collectBtnClick:(UIButton *)btn
{
    weakify(self);
    if (btn.selected) {
        [self deleteCollectWithid:self.baseModel.ID Success:^{
            btn.selected = NO;
            strongify(self);
            self.baseModel.is_collect = 1;
        } failed:^(NSString * _Nonnull error) {
            [QMUITips showWithText:error];
        }];
    }else
    {
        
        [self addCollectWithid:self.baseModel.ID imgUrl:self.baseModel.images template_type:self.baseModel.template_type Success:^{
            btn.selected = YES;
            strongify(self);
            self.baseModel.is_collect = 2;
        } failed:^(NSString * _Nonnull error) {
            [QMUITips showWithText:error];
        }];
    }
}




@end
